package aar.test.tessocr.view;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import aar.test.tessocr.utils.PrefUtils;
import clx.tessocr.ocreng.R;

/**
 * Created by 49776 on 2018/12/25.
 */

public class SearchHistoryPopwindow extends PopupWindow{

    private static final String SPLIT="@,";

    private Activity mActivity;

    private View view;

    private FlowLayout flowLayout;

    private boolean isShowBack;

    private String searchContent;

    private EditText etSearchContent;

    private OnSearchListener listener;

    private List<String> searchHistory=new ArrayList<>();

    private String type;

    public SearchHistoryPopwindow(Activity activity, String content, boolean isShowBack, OnSearchListener listener, String type){
        super(activity);
        this.mActivity=activity;
        this.isShowBack=isShowBack;
        this.listener=listener;
        this.type=type;
        this.searchContent=content;
        init();
        initView();
        initData();
    }

    public void init(){
        if (type.equals("orderSearchContent")) {
            view = mActivity.getLayoutInflater().inflate(R.layout.pop_search_history, null);

        } else {
            view = mActivity.getLayoutInflater().inflate(R.layout.pop_search_history, null);
            EditText et = (EditText) view.findViewById(R.id.et_search_content);
            et.setHint(mActivity.getResources().getString(R.string.hint_input_search_cabinet_content));
        }

        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        setHeight(LinearLayout.LayoutParams.MATCH_PARENT);
        setBackgroundDrawable(new BitmapDrawable(mActivity.getResources(), (Bitmap) null));
        setFocusable(true);
        setOutsideTouchable(false);
        setContentView(view);
    }
    public void initData(){
        String content = "";
        if (type.equals("orderSearchContent")) {
            content = PrefUtils.getString(mActivity, "orderSearchContent", "");
        } else {
            content = PrefUtils.getString(mActivity, "cabinetSearchContent", "");
        }

        if(!TextUtils.isEmpty(content)) {
            String[] contents = content.split(SPLIT);
            if (contents.length > 0) {
                for (int i = 0; i < contents.length; i++) {
                    final String search=contents[i];
                    searchHistory.add(contents[i]);
                    TextView textView = (TextView) LayoutInflater.from(mActivity).inflate(R.layout.item_flow_textview, null);
                    textView.setText(contents[i]);
                    textView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dismiss();
                            if(listener!=null)
                                listener.onSearch(search);
                        }
                    });
                    flowLayout.addView(textView);
                }
            }
        }

        etSearchContent.setText(searchContent);
        if(!TextUtils.isEmpty(searchContent)) {
            etSearchContent.setSelection(searchContent.length());
        }
    }

    public void initView(){
        flowLayout=view.findViewById(R.id.fl_search_record);
        view.findViewById(R.id.iv_clean_history).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (type.equals("orderSearchContent")) {
                    PrefUtils.setString(mActivity, "orderSearchContent", "");
                } else {
                    PrefUtils.setString(mActivity, "cabinetSearchContent", "");
                }

                flowLayout.removeAllViews();
            }
        });
        view.findViewById(R.id.ll_background).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SearchHistoryPopwindow.this.dismiss();
            }
        });
        view.findViewById(R.id.ll_content).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        view.findViewById(R.id.iv_back).setVisibility(isShowBack?View.VISIBLE:View.GONE);
        view.findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if(listener!=null){
                    listener.onBack();
                }
            }
        });
        etSearchContent= view.findViewById(R.id.et_search_content);
        etSearchContent.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId== EditorInfo.IME_ACTION_SEARCH ||(event!=null&&event.getKeyCode()== KeyEvent.KEYCODE_ENTER)){
                    if(listener!=null){
                        listener.onSearch(etSearchContent.getText().toString());
                        if(!TextUtils.isEmpty(etSearchContent.getText().toString())) {
                            checkDiff(etSearchContent.getText().toString());
                            saveSearchConten();
                        }
                        dismiss();
                    }
                    return true;
                }
                return false;
            }
        });

    }

    public void checkDiff(String content){
        int flag=0;
        for(int i=0;i<searchHistory.size();i++){
            if(!content.equals(searchHistory.get(i))){
                flag++;
            }
        }
        if(flag==searchHistory.size()){
            if(searchHistory.size()>=10){
                searchHistory.remove(searchHistory.size()-1);
            }
            searchHistory.add(0,content);
        }
    }

    public void saveSearchConten(){
        String content="";
        for(int i=0;i<searchHistory.size();i++){
            if(i==0)
                content=searchHistory.get(0);
            else{
                content=content+SPLIT+searchHistory.get(i);
            }
        }
        if (type.equals("orderSearchContent")) {
            PrefUtils.setString(mActivity, "orderSearchContent", content);
        } else {
            PrefUtils.setString(mActivity, "cabinetSearchContent", content);
        }

    }

    private void bgAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        mActivity.getWindow().setAttributes(lp);
    }

    public interface OnSearchListener{
        void onSearch(String searchContent);
        void onBack();
    }

}
